package com.wf.day_0618.test2;

import com.wf.day_0618.test1.entity.Person;
import com.wf.day_0618.test1.entity.Student;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class BeanFactory {
    private static Map<String, Object> map =new HashMap<>();


    static
    {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(Objects.requireNonNull(BeanFactory.class.getResourceAsStream("objects.txt"))));
        String s = null;
        try {
            while ((s = reader.readLine()) != null) {

                String[] split = s.split("=");
                Class<?> aClass = null;
                Object obj = null;
                if(Objects.equals(split[0], "className"))
                {
                    aClass = Class.forName(split[1]);
                    obj = aClass.newInstance();
                }

                while ((s = reader.readLine())!=null&&!(s).contains("#"))
                {
                    String[] split1 = s.split("=");

                    Field field = aClass.getDeclaredField(split1[0]);

                    field.setAccessible(true);

                    // 获取字段的类型
                    Class<?> type = field.getType();
                    System.out.println("Field type: " + type);

                    // 根据字段类型设置值
                    if (type == String.class) {
                        field.set(obj, split1[1]); // 字符串类型
                    } else if (type == int.class || type == Integer.class) {
                        field.setInt(obj, Integer.parseInt(split1[1])); // 整数类型
                    } else if (type == double.class || type == Double.class) {
                        field.setDouble(obj, Double.parseDouble(split1[1])); // 双精度浮点数类型
                    } else if (type == boolean.class || type == Boolean.class) {
                        field.setBoolean(obj, Boolean.parseBoolean(split1[1])); // 布尔类型
                    } else {
                        // 其他类型处理
                        System.out.println("Unsupported field type: " + type);
                    }

                }

                map.put(aClass.getSimpleName(),obj);
            }
        } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException |
                 NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        BeanFactory beanFactory = new BeanFactory();
     /*   beanFactory.loadingFactory();*/
        Student student = beanFactory.getBean("Student", Student.class);
        Person person = beanFactory.getBean("Person", Person.class);

        System.out.println(student);
        System.out.println(person);
    }

    public <T> T getBean(String key, Class<T> type) {

        if (map.containsKey(key)) {
            Object o = map.get(key);
            return (T) o;
        }

        return null;
    }

}